package com.feature50.makeover.utils;

import org.jdesktop.jdic.desktop.Desktop;

import java.io.File;
import java.lang.reflect.Method;

public class NativeUtils {
    public enum OS { Windows, Mac, Unknown }

    /**
     * Opens the passed file. I would just use JDIC but I'm very confused as to how to obtain JDIC builds for OS X
     * at the moment. So, this wrapper.
     *
     * @param file
     * @throws Exception
     */
    public static void openFile(File file) throws Exception {
        if (Platform.ISJAVA6)
        {
//            java.awt.Desktop.getDesktop().open(file);
            Class desktopCls = Class.forName("java.awt.Desktop");
            Object desktop = desktopCls.getMethod("getDesktop").invoke(null);
            desktop.getClass().getMethod("open", File.class).invoke(desktop, file);
            return;
        }


        OS os = getOS();
        switch (os) {
            case Mac:
                Class fm = Class.forName("com.apple.eio.FileManager");
                Method openURL = fm.getMethod("openURL", String.class);
                openURL.invoke(null, file.toURI().toString());
                break;
            case Unknown:
            case Windows:
                Desktop.open(file);
        }
    }

    public static void printFile(File file) throws Exception {
        OS os = getOS();
        switch (os) {
            case Mac:
            case Unknown:
            case Windows:
                Desktop.print(file);
        }
    }

    public static OS getOS() {
        String os = System.getProperty("os.name");
        if ("Mac OS X".equals(os)) {
            return OS.Mac;
        } else if (os.indexOf("Win") != -1) {
            // fixme: improve the windows test if necessary
            return OS.Windows;
        } else {
            return OS.Unknown;
        }
    }
}
